import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:mall/modules/model/goods_entity.dart';
import 'package:mall/router/navigator_util.dart';
import 'goods_widget.dart';

class GoodsListWidget extends StatefulWidget {
  const GoodsListWidget({
    super.key,
    required this.productList,
  });

  final List<GoodsEntity> productList;
  @override
  State<GoodsListWidget> createState() => _GoodsListWidgetState();
}

class _GoodsListWidgetState extends State<GoodsListWidget> {
  @override
  Widget build(BuildContext context) {
    return GridView.builder(
        physics: NeverScrollableScrollPhysics(),
        padding: EdgeInsets.all(0),
        shrinkWrap: true,
        itemCount: widget.productList.length,
        gridDelegate: MySliverGridDelegate(
          fixedHeight: 94,
          crossAxisCount: 2,
          childAspectRatio: 1,
          crossAxisSpacing: 10,
          mainAxisSpacing: 10,
        ),
        itemBuilder: (BuildContext context, int index) {
          return _getGridViewItem(context, widget.productList[index]);
        });
  }

  Widget _getGridViewItem(BuildContext context, GoodsEntity productEntity) {
    return GoodsWidget(productEntity, (value) {
      _goGoodsDetail(context, value);
    });
  }

  _goGoodsDetail(BuildContext context, int goodsId) {
    NavigatorUtil.goGoodsDetails(context, goodsId);
  }
}

class MySliverGridDelegate extends SliverGridDelegate {
  final double fixedHeight;

  /// The number of children in the cross axis.
  final int crossAxisCount;

  /// The number of logical pixels between each child along the main axis.
  final double mainAxisSpacing;

  /// The number of logical pixels between each child along the cross axis.
  final double crossAxisSpacing;

  /// The ratio of the cross-axis to the main-axis extent of each child.
  final double childAspectRatio;

  MySliverGridDelegate({
    this.fixedHeight = 0,
    required this.crossAxisCount,
    this.mainAxisSpacing = 0,
    this.crossAxisSpacing = 0,
    this.childAspectRatio = 1,
  });

  @override
  SliverGridLayout getLayout(SliverConstraints constraints) {
    // final childMainAxisExtent = childAspectRatio * constraints.crossAxisExtent / crossAxisCount;
    // final childCrossAxisExtent = childMainAxisExtent / childAspectRatio;
    final childCrossAxisExtent =
        (constraints.crossAxisExtent - (crossAxisCount - 1) * crossAxisSpacing) / crossAxisCount;
    final childMainAxisExtent = childCrossAxisExtent * childAspectRatio + fixedHeight;
    final mainAxisExtent = childMainAxisExtent + mainAxisSpacing;

    return SliverGridRegularTileLayout(
      crossAxisCount: crossAxisCount,
      mainAxisStride: mainAxisExtent,
      crossAxisStride: childCrossAxisExtent + crossAxisSpacing,
      childMainAxisExtent: childMainAxisExtent,
      childCrossAxisExtent: childCrossAxisExtent,
      reverseCrossAxis: axisDirectionIsReversed(constraints.crossAxisDirection),
    );
  }

  @override
  bool shouldRelayout(covariant MySliverGridDelegate oldDelegate) {
    return fixedHeight != oldDelegate.fixedHeight;
  }
}
